package cn.bocweb.spinning.view.common;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.support.annotation.Nullable;

import cn.bocweb.spinning.model.Bean.SecondHandle2Bean;
import cn.bocweb.spinning.model.Imp.BaseModelImp;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import retrofit.Callback;
import retrofit.Response;
import retrofit.Retrofit;

/**
 * Created by chenchangxing on 16/7/28.
 */
public class ReportSecondService extends Service {
    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
     * @param name Used to name the worker thread, important only for debugging.
     */
    String detail;
    String testid;
    String timeline;
    String userid;
    BaseModelImp baseModelImp =  new BaseModelImp();

    @Override
    public void onDestroy() {
        super.onDestroy();
        DeviceUtil.Log("上传数据的服务被杀死了");
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        timeline = String.valueOf(System.currentTimeMillis());

        testid = intent.getStringExtra(Constant.TO_REPORT_SECOND_TESTID);
        detail = intent.getStringExtra(Constant.TO_REPORT_SECOND_DETAIL);
        userid = intent.getStringExtra(Constant.TO_REPORT_SECOND_USERID);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    long time_sleep = (long) (Math.random() * 60) * 1000;
                    Thread.sleep(time_sleep);

                    baseModelImp
                            .mServer
                            .secondHandle2(timeline,
                                    DeviceUtil.getSign(timeline),
                                    userid,
                                    testid,
                                    detail)
                            .enqueue(new Callback<SecondHandle2Bean>() {
                                @Override
                                public void onResponse(Response<SecondHandle2Bean> response, Retrofit retrofit) {
                                    SharedPreferences sp = getSharedPreferences(Constant.SECOND_UPDATE, Context.MODE_PRIVATE);
                                    SharedPreferences.Editor editor = sp.edit();
                                    editor.putBoolean(Constant.SECOND_UPDATE_SUCCESS, true);
                                    editor.apply();
                                }

                                @Override
                                public void onFailure(Throwable t) {
                                    SharedPreferences sp = getSharedPreferences(Constant.SECOND_UPDATE, Context.MODE_PRIVATE);
                                    SharedPreferences.Editor editor = sp.edit();
                                    editor.putBoolean(Constant.SECOND_UPDATE_SUCCESS, false);
                                    editor.apply();
                                }
                            });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        return START_NOT_STICKY;
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
        DeviceUtil.Log("TakRemoved");
    }
}
